import tensorflow as tf

# mnist = tf.keras.datasets.mnist
#
# (x_train, y_train), (x_test, y_test) = mnist.load_data()
# x_train, x_test = x_train / 255.0, x_test / 255.0
#
# model = tf.keras.models.Sequential([
#   tf.keras.layers.Flatten(input_shape=(28, 28)),
#   tf.keras.layers.Dense(128, activation='relu'),
#   tf.keras.layers.Dropout(0.2),
#   tf.keras.layers.Dense(10, activation='softmax')
# ])
#
# model.compile(optimizer='adam',
#               loss='sparse_categorical_crossentropy',
#               metrics=['accuracy'])
#
# model.fit(x_train, y_train, epochs=5)
#
# model.evaluate(x_test,  y_test, verbose=2)

import numpy as np
import pandas as pd
import os
import keras
from keras.models import Sequential
from keras.layers import Conv2D, MaxPool2D, AveragePooling2D
from keras.layers import Dense, Activation, Dropout, Flatten
from keras.preprocessing import image
from keras.preprocessing.image import ImageDataGenerator
import matplotlib.pyplot as plt

filename = '../src/fer2013.csv'

label_classes = ['Anger', 'Disgust', 'Fear', 'Happy', 'Sad', 'Surprise', 'Neutral']
names = ['emotion', 'pixels', 'usage']
df = pd.read_csv('../src/fer2013.csv', names=names, na_filter=False)

im = df['pixels']
df.head(10)

def getData(filename):
    # 图片都是 48x48
    # 数据量 35888
    Y = []
    X = []
    flag = True
    for line in open(filename):
        if flag:
            flag = False
        else:
            row = line.split(',')
            Y.append(int(row[0]))
            X.append([int(p) for p in row[1].split()])
    X, Y = np.array(X) / 255.0, np.array(Y)
    return X, Y


X, Y = getData(filename)
# 将表情数据分类提取出来 7类
num_class = len(set(Y))

N, D = X.shape
X = X.reshape(N, 48, 48, 1)

from sklearn.model_selection import train_test_split

X_train, X_Test, Y_train, Y_Test = train_test_split(X, Y, test_size=0.1, random_state=0)
Y_train = (np.arange(num_class) == Y_train[:, None]).astype(np.float32)
Y_Test = (np.arange(num_class) == Y_Test[:, None]).astype(np.float32)

print("X_train:", X_train)
print('***********************')

print("X_Test:", X_Test)
print('***********************')
print("Y_train:", Y_train)
print('***********************')
print("Y_Test:", Y_Test)
print('***********************')




























